package org.shiro.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文工具类
 *
 * @author 党怡承
 * @version 1.0
 * @date 2021-04-24 13:43
 */
@Component
public class SpringUtils implements ApplicationContextAware {
  private static ApplicationContext context;
  /**
   * Spring在bean初始化后会判断是不是ApplicationContextAware的子类 *
   * 如果该类是,setApplicationContext()方法,会将容器中ApplicationContext作为参数传入进去
   *
   * @param applicationContext
   * @throws BeansException
   */
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    context = applicationContext;
  }

  /**
   * 通过Name返回指定的bean
   *
   * @param clazz
   * @param <T>
   * @return
   */
  public static <T> T getBean(Class<T> clazz) {
    return context.getBean(clazz);
  }
}
